package com.cp_smss.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cp_smss.common.GlobalConstants;
import com.cp_smss.dao.UserDao;
import com.cp_smss.model.Page;
import com.cp_smss.model.PageModel;
import com.cp_smss.model.User;
import com.cp_smss.service.UserService;
import com.cp_smss.util.AuthProcesser;
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;
	
	@Override
	public int addUser(User user) {
		if(null != getUserByEmail(user.getEmail())){
			return 0;
		}
		user.setPassword(AuthProcesser.make(user.getPassword()));
		user.setEnable(GlobalConstants.USER_ENABLE);
		return userDao.addUser(user);
	}

	public User getUserByEmail(String email){
		User user = new User();
		user.setEmail(email);
		return userDao.getUserByCondition(user);
	}
	
	@Override
	public User getUserById(String userId) {
		return userDao.getUserById(userId);
	}

	@Override
	public int updateUser(User user) {
		return userDao.updateUser(user);
	}

	@Override
	public int delUser(String userId) {
		User user = new User();
		user.setId(Integer.valueOf(userId));
		if(null == userDao.getUserByCondition(user)){
			return userDao.delUser(userId);
		}
		return 0;
	}

	@Override
	public PageModel<User> listUsers(int pageNo,int pageSize){
		return userDao.listUsers(pageNo,pageSize);
	}

	@Override
	public boolean checkDuplicate(User user) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public User getUserByCondition(User user) {
		return userDao.getUserByCondition(user);
	}

}
